package com.intelligent.ispc.foundation.poi;

import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.utils.ResourceBundleUtils;
import com.intelligent.ispc.foundation.pdf.TableStyleConfig;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by Eligi.Ran on 2016/9/5.
 */
public class PoiComponentFactory {
    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;

    public static Integer paintImage(Workbook wb, String imagePath) {
        InputStream is = null;
        Integer pictureIdx = null;
        try {
            is = new FileInputStream(imagePath);
            byte[] bytes = IOUtils.toByteArray(is);
            pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return pictureIdx;
    }

    public static XSSFSheet paintTable(List<String> metadatas, Object[][] values, TableStyleConfig styleConfig, Map<Integer, String> mark) {
        if (metadatas == null || values == null || styleConfig == null) {
            throw new ApplicationException(ExceptionMessages.ERR_15005, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_EXPORT_STRUCTURE_INVALID));
        }

        int rowNum = values.length;
        int colNum = values[0].length;
        int metadatasNum = metadatas.size();
        int tableColNum;
        if (colNum != metadatasNum) {
            throw new ApplicationException(ExceptionMessages.ERR_15006, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_EXPORT_DATAS_INVALID));
        }

        int arrangeStyle = styleConfig.getCurrentLayout();
        XSSFWorkbook workbook = new XSSFWorkbook();
        if (arrangeStyle == HORIZONTAL) {
            XSSFSheet reusltSheet = workbook.createSheet();
            XSSFTable table = reusltSheet.createTable();
            CTTable cttable = table.getCTTable();
            AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(rowNum, metadatasNum - 1));
            cttable.setRef(reference.formatAsString());
            cttable.setId(1);
            cttable.setName("SingleTable");
            cttable.setTotalsRowCount(1);
            Row metadataRow = reusltSheet.createRow(0);
            //set filed names
            Cell fiedCell;
            int fieldCounter = 0;
            for (String metadata : metadatas) {
                fiedCell = metadataRow.createCell(fieldCounter++);
                fiedCell.setCellValue(metadata);
            }
            //set value for each field
            Row dataRow;
            for (int i = 0; i < rowNum; i++) {
                dataRow = reusltSheet.createRow(i + 1);
                Cell cell;
                for (int j = 0; j < colNum; j++) {
                    cell = dataRow.createCell(j);
                    cell.setCellValue(values[i][j].toString());
                }
            }
            return reusltSheet;

        } else if (arrangeStyle == VERTICAL) {
            XSSFSheet reusltSheet = workbook.createSheet();
            XSSFTable table = reusltSheet.createTable();
            CTTable cttable = table.getCTTable();
            AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(metadatasNum - 1, rowNum));
            cttable.setRef(reference.formatAsString());
            cttable.setId(1);
            cttable.setName("SingleTable");
            cttable.setTotalsRowCount(1);

            CellStyle metadataCellStyle = workbook.createCellStyle();
            metadataCellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
            metadataCellStyle.setVerticalAlignment(VerticalAlignment.JUSTIFY);
            metadataCellStyle.setWrapText(true);
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
            cellStyle.setVerticalAlignment(VerticalAlignment.JUSTIFY);
            cellStyle.setWrapText(true);
            Font font;

            Row dataRow;
            Cell fieldCell;
            Cell cell;
            String value;
            for (int i = 0; i < colNum; i++) {
                dataRow = reusltSheet.createRow(i);
                fieldCell = dataRow.createCell(0);
                if (mark != null && mark.get(-1) == "first") {
                    font = workbook.createFont();
                    cellStyle.setFont(font);
                    String color = mark.get(i);
                    if (color != null) {
                        if (color == "RED") {
                            font.setColor(HSSFColor.RED.index);
                        }
                        fieldCell.setCellStyle(cellStyle);
                    }
                }
                if (mark != null && mark.get(-2) == "first") {
                    font = workbook.createFont();
                    metadataCellStyle.setFont(font);
                    font.setBold(true);
                    fieldCell.setCellStyle(metadataCellStyle);
                }
                fieldCell.setCellValue(metadatas.get(i));
                for (int j = 0; j < rowNum; j++) {
                    cell = dataRow.createCell(j + 1);
                    value = values[j][i].toString();
                    if (mark != null && mark.get(-1) == "second") {
                        font = workbook.createFont();
                        cellStyle.setFont(font);
                        String color = mark.get(i);
                        if (color != null) {
                            if (color == "GREEN") {
                                font.setColor(HSSFColor.GREEN.index);
                            }
                            cell.setCellStyle(cellStyle);
                        }
                    }
                    cell.setCellValue(value);
                }
            }
            return reusltSheet;
        }
        return null;
    }

}
